package com.chidopi.market.bookstore.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.bookstore.domain.Brand;
import com.chidopi.market.bookstore.domain.ContextProvider;

public class ContextProviderDaoImpl extends AbstractBaseDaoImpl<ContextProvider>  {
	
	private static Logger logger = LoggerFactory.getLogger(ContextProviderDaoImpl.class);

	@Override
	public int insert(ContextProvider t) {
		String sql = "insert into contextprovider (CPNAME, CPDESCRIPTION, CPCODE, BRAND_ID, CREATE_DATE, CREATE_BY) "
				+ "values (:cpName, :cpDescription, :cpCode, :brand.id ,NOW(), :create_by)";
		return super.execSQL(sql, t);
	}

	@Override
	public int update(ContextProvider t) {
		String sql = "update contextprovider set CPNAME = :cpName, CPDESCRIPTION = :cpDescription, CPCODE = :cpCode, BRAND_ID=:brand.id, update_by = :update_by where id= :id";
		return super.execSQL(sql, t);
	}

	@Override
	public int delete(ContextProvider t) {
		String sql = "delete from contextprovider where id = :id";
		return super.execSQL(sql, t);
	}

	@Override
	public List<ContextProvider> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select c.*, b.ID BRAND_ID, b.BRANDNAME  from contextprovider c left join brand b on c.brand_id= b.id order by c.id ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select c.*, b.ID BRAND_ID, b.BRANDNAME from contextprovider c left join brand b on c.brand_id= b.id where " + sqlWhere + " order by c.id";
			logger.debug("sql: " + sql);
			List<ContextProvider> list = super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
			if (list == null) {
				logger.debug("query null!!!");
			} else {
				for (ContextProvider provider : list) {
					logger.debug("id=" + provider.getId() + "/cpName=" + provider.getCpName());
				}
			}
			return list;
		}
	}

	@Override
	public ContextProvider findById(Object id) {
		String sql = "c.id=?";
		List<ContextProvider> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	@Override
	protected String getTable() {
		return "contextprovider";
	}

	@Override
	protected String getSequenceName() {
		return null;
	}

	@Override
	protected RowMapper<ContextProvider> getRowMapper() {
//		return ParameterizedBeanPropertyRowMapper.newInstance(ContextProvider.class);
		return new ParameterizedRowMapper<ContextProvider>(){

			@Override
			public ContextProvider mapRow(ResultSet rs, int rowNum)	throws SQLException {
				ContextProvider vo = new ContextProvider();
				vo.setId(rs.getInt("ID"));
				vo.setCpName(rs.getString("CPNAME"));
				vo.setCpDescription(rs.getString("CPDESCRIPTION"));
				vo.setCpCode(rs.getString("CPCODE"));
				Brand brand = new Brand();
				brand.setId(rs.getInt("BRAND_ID"));
				brand.setBrandname(rs.getString("BRANDNAME"));
				vo.setBrand(brand);
//				vo.setCreate_date(rs.getDate("CREATE_DATE"));
//	            vo.setCreate_by(rs.getInt("CREATE_BY"));
//	            vo.setUpdate_date(rs.getDate("UPDATE_DATE"));
//	            vo.setUpdate_by(rs.getInt("UPDATE_BY"));
				return vo;
			}
		};	
    }

}
